package com.ouc.openplatform.ouc.authManage.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.ouc.openplatform.ouc.authManage.dto.NJ_AuthorityInfoDTO;
import com.ouc.openplatform.ouc.authManage.model.NJ_AuthorityInfo;
import com.ouc.openplatform.ouc.authManage.service.NJ_AuthorityInfoService;
import com.ouc.openplatform.ouc.authManage.service.NJ_ClientAuthorityInfoService;
import com.ouc.openplatform.pojo.EUDataGridResult;

public class NJ_ClientAuthorityInfoServiceImpl implements NJ_ClientAuthorityInfoService {
	private NJ_AuthorityInfoService nj_authorityInfoService;

	public NJ_AuthorityInfoService getNj_authorityInfoService() {
		return nj_authorityInfoService;
	}

	public void setNj_authorityInfoService(NJ_AuthorityInfoService nj_authorityInfoService) {
		this.nj_authorityInfoService = nj_authorityInfoService;
	}

	// 1.保存权限信息-----sxl
	@Override
	public int saveAuthorityInfo(NJ_AuthorityInfoDTO authorityInfoDTO) {
		NJ_AuthorityInfo authorityInfo = new NJ_AuthorityInfo();
		try {
			BeanUtils.copyProperties(authorityInfo, authorityInfoDTO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		int flag = nj_authorityInfoService.saveAuthorityInfo(authorityInfo);
		return flag;
	}

	// 2.删除权限信息-----sxl
	@Override
	public int deleteAuthorityInfo(int id0) {
		int flag = 0;
		List<NJ_AuthorityInfo> authorityList = nj_authorityInfoService.getAuthorityInfoByParentid(id0);
		if (authorityList.size() == 0 || authorityList == null) {
			flag = nj_authorityInfoService.deleteAuthorityInfo(id0);
		} else {
			flag = 2;
		}
		return flag;
	}

	// 3.修改权限信息-----sxl
	@Override
	public int updateAuthorityInfo(NJ_AuthorityInfoDTO authorityInfoDTO) {
		int flag = nj_authorityInfoService.updateAuthorityInfo(authorityInfoDTO);
		return flag;
	}

	// 4.根据条件获取权限信息-----sxl
	@Override
	public EUDataGridResult getAllAuthorityInfo_DataGrid(NJ_AuthorityInfoDTO nj_authorityinfoDTO, int page, int rows) {
		return nj_authorityInfoService.getAllAuthorityInfo(null, page, rows);
	}

	// 5.获取所有的权限信息-----sxl
	@Override
	public List<NJ_AuthorityInfo> getAuthorityInfo(NJ_AuthorityInfo authorityinfo) {
		// 特殊处理实体类中int类型的字段
		return nj_authorityInfoService.getAuthorityInfo(authorityinfo);
	}

	// 4.获取所有的权限信息(treegrid专用)-----sxl
	@Override
	public List<NJ_AuthorityInfoDTO> getAllAuthorityInfo_Treegrid(NJ_AuthorityInfoDTO authorityinfoDTO) {
		List<NJ_AuthorityInfoDTO> nj_authorityInfoDTOList = new ArrayList<NJ_AuthorityInfoDTO>();
		List<NJ_AuthorityInfo> nj_authorityInfoList = nj_authorityInfoService.getAuthorityInfo(null);
		for (NJ_AuthorityInfo nj_authorityInfo : nj_authorityInfoList) {
			NJ_AuthorityInfoDTO authorityInfoDTO = new NJ_AuthorityInfoDTO();
			authorityInfoDTO.setState("open");
			try {
				BeanUtils.copyProperties(authorityInfoDTO, nj_authorityInfo);
			} catch (Exception e) {
				e.printStackTrace();
			}
			nj_authorityInfoDTOList.add(authorityInfoDTO);
		}
		return nj_authorityInfoDTOList;
	}

	// 根据父级id获取所有权限信息-------sxl
	@Override
	public List<NJ_AuthorityInfoDTO> getAllAuthorityInfoByParentid(int parentid) {
		List<NJ_AuthorityInfoDTO> nj_authorityInfoDTOList = new ArrayList<NJ_AuthorityInfoDTO>();
		List<NJ_AuthorityInfo> nj_authorityInfoList = nj_authorityInfoService.getAuthorityInfoByParentid(parentid);
		for (NJ_AuthorityInfo nj_authorityInfo : nj_authorityInfoList) {
			NJ_AuthorityInfoDTO authorityInfoDTO = new NJ_AuthorityInfoDTO();
			try {
				BeanUtils.copyProperties(authorityInfoDTO, nj_authorityInfo);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (authorityInfoDTO.getFlag().equals("1"))
				authorityInfoDTO.setFlagname("是");
			else {
				authorityInfoDTO.setFlagname("否");
			}
			nj_authorityInfoDTOList.add(authorityInfoDTO);
		}
		return nj_authorityInfoDTOList;
	}

}
